package com.aca.impl;

import com.aca.dao.SceneDAO;
import com.aca.entity.Admin;
import com.aca.entity.Role;
import com.aca.entity.Scene;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class SceneDAOImpl extends HibernateDaoSupport implements SceneDAO {

    @Override
    public void addScene(Scene scene) {
        getHibernateTemplate().save(scene);
    }

    @Override
    public void updateScene(Scene scene) {
        getHibernateTemplate().update(scene);
    }

    @Override
    public void delScene(String name) {
        getHibernateTemplate().delete(findScene(name));
    }

    @Override
    public Scene findScene(String name) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Scene where sname=?1";
        Query query = session1.createQuery(hql);
        List<Scene> list = query.setParameter(1,name).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size() > 0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List pageScene(int pageCur, int pageSize, Scene scene) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "";
        if (scene == null){
            hql = "from Scene where like '%" + scene.getSname().trim()
                    + "%' order by sname";
        }
        Query query = session1.createQuery(hql);
        int firstRes = (pageCur - 1) * pageSize;
        query.setFirstResult(firstRes);
        query.setMaxResults(pageSize);
        List list = query.list();
        transaction.commit();
        session1.close();
        session1 = null;
        return list;
    }

    @Override
    public int getSceneTol() {
        return findAllScene().size();
    }

    @Override
    public List findAllScene() {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Scene";
        Query query = session1.createQuery(hql);
        List<Scene> list = query.list();

        transaction.commit();
        session1.close();
        session1 = null;

        return list;
    }

    @Override
    public Scene findSceneBySno(int sno) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Scene where sno=?1";
        Query query = session1.createQuery(hql);
        List<Scene> list = query.setParameter(1,sno).list();

        transaction.commit();
        session1.close();
        session1 = null;


        if (list.size() > 0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public boolean existScene(String name) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Scene where sname=?1";
        Query query = session1.createQuery(hql);
        List<Scene> list = query.setParameter(1,name).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size() > 0){
            return true;
        }else {
            return false;
        }
    }
}
